package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-10-16
 * Time: 17:21
 */
//使用这个servlet返回主页信息
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //要做的工作就是要把当前的用户信息展示到页面上
        HttpSession session = req.getSession(false);
        if(session == null){
            //用户未登录, 则跳转到登录页面, 要求用户重新登录
            resp.sendRedirect("login.html");
            return;
        }
        //已经登录成功就获取到会话中的数据
        //由于 getAttribute 返回的是Object, 所以需要手动改成String
        String username = (String)session.getAttribute("username");
        Integer visitCount = (Integer)session.getAttribute("visitCount");
        visitCount = visitCount + 1;
        session.setAttribute("visitCount", visitCount);

        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("当前用户为:" + username + " 访问次数:" + visitCount);

    }
}
